package com.lark.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

@Slf4j
public final class PropertiesUtil {

    /**
     * 读取resource下的配置文件
     *
     * @param filePath
     * @return
     */
    public static Properties parseResourceFile(String filePath) {
        Properties properties = new Properties();
        try (InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);
             InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
            // 加载.properties文件（UTF-8编码）
            properties.load(reader);
        } catch (IOException e) {
            log.error("读取配置文件:[{}]失败", filePath, e);
        }
        return properties;
    }

    /**
     * 解析非resource下的配置文件
     *
     * @param file
     * @return
     */
    public static Properties parseFile(File file) {
        Properties properties = new Properties();
        try (InputStream in = new FileInputStream(file);
             InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
            // 加载.properties文件（UTF-8编码）
            properties.load(reader);
        } catch (IOException e) {
            log.error("读取配置文件:[{}]失败", file.getAbsoluteFile(), e);
        }
        return properties;
    }
}
